<template>
  <el-dialog
    title="新增流程定义"
    :visible.sync="show"
    width="900px"
    center
    append-to-body
  >
    <el-row style="margin-bottom:20px;padding-top:10px;">
      <el-col :span="24">
        <el-input v-model="addObjname" placeholder="请输入流程定义名称">
          <template slot="prepend">流程定义名称：</template>
        </el-input>
      </el-col>
    </el-row>
    <div class="content-box">
      <div class="left-tree">
        <el-table
          ref="tabletreepeople"
          :data="treeData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          highlight-current-row
          default-expand-all
          :tree-props="{children: 'nodes', hasChildren: 'hasChildren'}"
          @current-change="handleNodeClick"
        >
          <el-table-column
            prop="name"
            label="组织机构"
          />
        </el-table>
      </div>
      <div class="right-content">
        <div class="top-table-content">
          <com-table
            highlight-current-row
            :data="tableData"
            :columns="tableColumn"
            :total="total"
            :defaultrow="-1"
            @pageChange="pageChange"
            @change="handleNodeClick1"
          >
            <template v-slot:sex="{row: {sex}}">
              {{ sex ? '男' : '女' }}
            </template>
          </com-table>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { otherPriceDescTypes } from '@/utils/dicData'
import {
  getOrganizationTree,
  getUserList
} from '@/api/manage'
export default {
  name: 'MaterialListModal',
  components: {

  },
  props: {
    title: {
      type: String,
      default: ''
    },
    showModal: {
      type: Boolean,
      default: false
    },
    columns: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      otherPriceDescTypes,
      visible: false,
      total: 0,
      pageParams: {
        pageNum: 1,
        pageSize: 10
      },
      treeData: [], // 树列表
      tableData: [], // 表格列表
      selectRow: null, // 选中的数据
      directCurrentNode: null, // 选中的目录
      addObjname: null,
      tableColumn: [
        {
          label: '姓名',
          prop: 'name'
        },
        {
          label: '账号',
          prop: 'account'
        },
        {
          label: '工号',
          prop: 'number'
        },
        {
          label: '性别',
          prop: 'sex',
          slot: 'sex'
        }
      ]
    }
  },
  computed: {
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal: {
      handler(newData) {
        if (newData) {
          this.addObjname = null
          this.selectRow = null
          this.initTree()
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {},
  methods: {
    confirm() {
      if (!this.addObjname) return this.$message.error('请输入流程定义名称！')
      this.show = false
      if (this.directCurrentNode || (this.directCurrentNode && this.selectRow)) {
        this.$emit('getSelectedRows', {
          directCurrentNode: this.directCurrentNode,
          selectRow: this.selectRow,
          name: this.addObjname
        })
      }
    },
    pageChange(data) {
      this.pageParams = data
      this.getTableList(this.directCurrentNode)
    },
    // 获取树结构目录
    initTree() {
      getOrganizationTree().then(res => {
        const { nodes } = res.data || {}
        this.treeData = nodes || []
        if (nodes && nodes.length > 0) {
          this.$nextTick(() => {
            this.$refs.tabletreepeople.setCurrentRow(nodes[0])
          })
          this.directCurrentNode = nodes[0]
          this.getTableList(nodes[0])
        }
      })
    },
    // 目录相关
    handleNodeClick(val) {
      // 拿到机构信息
      this.directCurrentNode = val
      this.getTableList(val)
    },
    // 拿到具体的人员信息
    handleNodeClick1(val) {
      // if (val.organizationId === this.selectRow.organizationId && val.id === this.selectRow.id) {
      //   this.defaultrow = -1
      //   return
      // }
      this.selectRow = val
    },
    // 获取材料表格数据
    getTableList(data) {
      const { id } = data
      if (id) {
        const params = {
          ...this.pageParams,
          organizationId: id
        }
        getUserList(params).then(res => {
          const { data, total } = res.data || {}
          if (data) {
            this.tableData = res.data.data
            this.total = total - 0
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
::v-deep .content-box{
  overflow: hidden;
  .left-tree{
    width: 200px;
    float: left;
    max-height:600px;
    overflow-y: auto;
  }
  .right-content{
    width: calc(100% - 210px);
    float: right;
    .top-table-content{
      min-height: 200px;
    }
    // .action-btns{
    //   padding: 10px;
    // }
    .bottom-table-content{
      min-height: 200px;
    }
  }
}
</style>
